<?php

declare(strict_types=1);
/**
 * This file is part of MoChat.
 * @link     https://mo.chat
 * @document https://mochat.wiki
 * @contact  group@mo.chat
 * @license  https://github.com/mochat-cloud/mochat/blob/master/LICENSE
 */
namespace MoChat\Plugin\ContactTransfer\Logic;

use EasyWeChat\Kernel\Exceptions\InvalidConfigException;
use GuzzleHttp\Exception\GuzzleException;
use Hyperf\Contract\StdoutLoggerInterface;
use Hyperf\Di\Annotation\Inject;
use MoChat\App\Corp\Contract\CorpContract;
use MoChat\App\Corp\Logic\AppTrait;
use MoChat\App\WorkContact\Contract\WorkContactContract;
use MoChat\App\WorkContact\Contract\WorkContactEmployeeContract;
use MoChat\App\WorkContact\Contract\WorkContactTagContract;
use MoChat\App\WorkContact\Contract\WorkContactTagPivotContract;
use MoChat\App\WorkEmployee\Contract\WorkEmployeeContract;
use MoChat\App\WorkMessage\Contract\WorkMessageContract;
use MoChat\Framework\Constants\ErrorCode;
use MoChat\Framework\Exception\CommonException;
use MoChat\Plugin\ContactTransfer\Contract\WorkTransferLogContract;
use MoChat\Plugin\ContactTransfer\Contract\WorkUnassignedContract;

/**
 * Class AllotLogic.
 */
class AllotLogic
{
    use AppTrait;

    /**
     * @Inject
     * @var WorkTransferLogContract
     */
    protected $workTransferLogService;

    /**
     * @Inject
     * @var CorpContract
     */
    protected $corpService;

    /**
     * @Inject
     * @var WorkUnassignedContract
     */
    protected $workUnassignedService;

    /**
     * @Inject
     * @var WorkEmployeeContract
     */
    protected $workEmployeeService;

    /**
     * @Inject
     * @var WorkContactEmployeeContract
     */
    protected $workContactEmployeeService;

    /**
     * @Inject
     * @var WorkContactContract
     */
    protected $workContactService;

    /**
     * @Inject
     * @var WorkContactTagContract
     */
    protected $workContactTagService;

    /**
     * @Inject
     * @var WorkContactTagPivotContract
     */
    protected $workContactTagPivotService;

    /**
     * @var WorkMessageContract
     */
    protected $workMessageService;

    /**
     * @Inject
     * @var StdoutLoggerInterface
     */
    private $logger;

    /**
     * 获分配在职或离职客户.
     * @param $params
     * @throws GuzzleException
     * @throws InvalidConfigException
     */
    public function handle($params): array
    {
        $wx = $this->wxApp($params['corpId'], 'contact')->external_contact;
        $result = [];
        // 离职分配
        if ($params['type'] === 1) {
            foreach ($params['list'] as $param) {// array $externalUserId, string $handoverUserId, string $takeoverUserId
                $temp = $wx->resignedTransferCustomer([$param['contactWxId']], $param['employeeWxId'], $params['takeoverUserId']);
                $result[] = $temp;
                if ($temp['errcode'] === 0) {
                    $contact = $this->workContactService->getWorkContactByCorpIdWxExternalUserId($params['corpId'], $param['contactWxId']);
                    $this->workTransferLogService->createWorkTransferLog([
                        'corp_id' => $params['corpId'],
                        'status' => 1,
                        'type' => 1,
                        'name' => $contact['name'] ?? '',
                        'contact_id' => $param['contactWxId'],
                        'handover_employee_id' => $param['employeeWxId'],
                        'takeover_employee_id' => $params['takeoverUserId'],
                        'state' => 2,
                    ]);
                } elseif ($temp['errcode'] === 84061) {
                    $this->logger->error('不存在外部联系人的关系' . $param['contactWxId'] . '-' . $param['employeeWxId']);
                    throw new CommonException(ErrorCode::SERVER_ERROR, '不存在外部联系人的关系:');
                } elseif ($temp['errcode'] === 40003) {
                    $this->logger->error('分配失败-成员未配置联系客户功能' . $params['takeoverUserId'] . json_encode($temp));
                    throw new CommonException(ErrorCode::SERVER_ERROR, '分配失败-成员未配置联系客户功能');
                }
            }
        }
        // 在职分配
        if ($params['type'] === 2) {
            foreach ($params['list'] as $param) {
                $temp = $wx->transferCustomer([$param['contactWxId']], $param['employeeWxId'], $params['takeoverUserId'], '');
                $result[] = $temp;
                if ($temp['errcode'] === 0) {
                    $contact = $this->workContactService->getWorkContactByCorpIdWxExternalUserId($params['corpId'], $param['contactWxId']);
                    $this->workTransferLogService->createWorkTransferLog([
                        'corp_id' => $params['corpId'],
                        'status' => 2,
                        'type' => 1,
                        'name' => $contact['name'] ?? '',
                        'contact_id' => $param['contactWxId'],
                        'handover_employee_id' => $param['employeeWxId'],
                        'takeover_employee_id' => $params['takeoverUserId'],
                    ]);
                } elseif ($temp['errcode'] === 84061) {
                    $this->logger->error('不存在外部联系人的关系' . $param['contactWxId'] . '-' . $param['employeeWxId']);
                    throw new CommonException(ErrorCode::SERVER_ERROR, '不存在外部联系人的关系:');
                } elseif ($temp['errcode'] === 40003) {
                    $this->logger->error('分配失败-成员未配置联系客户功能' . $params['takeoverUserId'] . json_encode($temp));
                    throw new CommonException(ErrorCode::SERVER_ERROR, '分配失败-成员未配置联系客户功能');
                }
            }
        }
        return $result;
    }
}
